package com.one.common.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.one.common.R;
import com.one.common.utils.StringUtils;


/**
 * creator: zhulunjun
 * time:    2018/7/8
 * describe: 个人中心，水平选项item
 */

public class HorizontalMenuItem extends LinearLayout {
    private ImageView ivIcon, ivRight, ivArrow;
    private TextView tvText, tvRightText;
    private EditText editRight;
    private LinearLayout llMenuItem;
    private View line;

    public HorizontalMenuItem(Context context) {
        super(context, null);
    }

    public HorizontalMenuItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        LayoutInflater.from(context).inflate(R.layout.menu_item_horizontal, this);
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalMenuItem);
        int iconRes = ta.getResourceId(R.styleable.HorizontalMenuItem_icon, -1);
        int rightRes = ta.getResourceId(R.styleable.HorizontalMenuItem_right_img, -1);
        String text = ta.getString(R.styleable.HorizontalMenuItem_text);
        String rightText = ta.getString(R.styleable.HorizontalMenuItem_right_text);
        boolean isEdit = ta.getBoolean(R.styleable.HorizontalMenuItem_is_edit,false);
        boolean isEnabled = ta.getBoolean(R.styleable.HorizontalMenuItem_is_enabled,true);
        boolean notLine = ta.getBoolean(R.styleable.HorizontalMenuItem_not_line,false);
        int lineColor = ta.getResourceId(R.styleable.HorizontalMenuItem_line_color, -1);
        boolean hasArrow = ta.getBoolean(R.styleable.HorizontalMenuItem_has_arrow,false);
        int parentPLeft = ta.getResourceId(R.styleable.HorizontalMenuItem_line_m_left, -1);
        setView(iconRes, text, rightText, rightRes, isEdit, isEnabled, notLine,lineColor,hasArrow,parentPLeft);
        ta.recycle();
    }


    private void setView(int icon, String text, String rightText, int rightImg, boolean isEdit,
                         boolean isEnabled, boolean notLine,int lineColor, boolean hasArrow, int parentPLeft) {
        ivIcon = (ImageView) findViewById(R.id.iv_left);
        ivRight = (ImageView) findViewById(R.id.iv_right);
        tvText = (TextView) findViewById(R.id.tv_text);
        tvRightText = (TextView) findViewById(R.id.tv_right);
        ivArrow = (ImageView) findViewById(R.id.iv_arrow);
        editRight = (EditText) findViewById(R.id.et_right);
        llMenuItem = (LinearLayout) findViewById(R.id.ll_menu_item);
        line = findViewById(R.id.line);

        setIsEdit(isEdit);
        setText(text);
        setRightTxt(rightText);
        setIcon(icon);
        setRightImg(rightImg);
        setIsEnabled(isEnabled);
        setLine(notLine);
        setLineColor(lineColor);
        hasArrow(hasArrow);
        setLineMLeft(parentPLeft);
    }

    public void setLineMLeft(int left){
        if(left != -1) {
            LinearLayout.LayoutParams params = (LayoutParams) line.getLayoutParams();
            params.leftMargin = (int) getResources().getDimension(left);
            line.setLayoutParams(params);
        }
    }

    public void setIsEdit(boolean isEdit){
        llMenuItem.setEnabled(!isEdit);
        if (isEdit){
            editRight.setVisibility(VISIBLE);
            ivArrow.setVisibility(INVISIBLE);
            tvRightText.setVisibility(GONE);
            if(StringUtils.isNotBlank(tvRightText.getText().toString())){
                editRight.setText(tvRightText.getText());
                editRight.setSelection(tvRightText.getText().length());
            }
        } else {
            editRight.setVisibility(GONE);
            ivArrow.setVisibility(VISIBLE);
            tvRightText.setVisibility(VISIBLE);
        }
    }

    public void setIsEnabled(boolean isEnabled){
        llMenuItem.setEnabled(isEnabled);
        if(isEnabled){
            ivArrow.setVisibility(VISIBLE);
        } else {
            ivArrow.setVisibility(INVISIBLE);
        }
    }

    /**
     * 设置线条颜色
     */
    public void setLineColor(int color){
        if(color!=-1) {
            line.setBackgroundColor(getResources().getColor(color));
        }
    }

    /**
     * 师傅显示像右箭头
     */
    public void hasArrow(boolean has){
        if (has){
            ivArrow.setVisibility(VISIBLE);
        }else {
            ivArrow.setVisibility(GONE);
        }
    }

    public void setLine(boolean notLine){
        if(notLine){
            line.setVisibility(GONE);
        } else {
            line.setVisibility(VISIBLE);
        }
    }

    public void setText(String text) {
        if (StringUtils.isNotBlank(text)) {
            tvText.setVisibility(VISIBLE);
            tvText.setText(text);
        }
    }

    public void setIcon(int icon) {
        if (icon != -1) {
            ivIcon.setVisibility(VISIBLE);
            ivIcon.setImageResource(icon);
        } else {
            ivIcon.setVisibility(GONE);
        }
    }

    public void setRightImg(int img) {
        if (img != -1) {
            ivRight.setVisibility(VISIBLE);
            ivRight.setImageResource(img);
        } else {
            ivRight.setVisibility(GONE);
        }
    }

    public void setRightTxt(String rightText) {
        if (StringUtils.isBlank(rightText)) {
            tvRightText.setVisibility(GONE);
        } else {
            tvRightText.setVisibility(VISIBLE);
            tvRightText.setText(rightText);
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            llMenuItem.setOnClickListener(onClickListener);
        }
    }

    public ImageView getIvRight() {
        return ivRight;
    }

    public EditText getEditRight() {
        return editRight;
    }

    public TextView getTvRightText(){
        return tvRightText;
    }
}
